﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Datorgrafik_lab1.Objects;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Datorgrafik_lab1.Helpers;

namespace Datorgrafik_lab1.Objects {
    public class Ground : GameObject{
        
        private float QuadRadius = 10;
        private int numQuads = 70;
        private Vector3 firstQuadPosition;

        public Ground(GraphicsDevice graphics, Vector3 pos, float scale)
            : base(graphics, pos, Quaternion.Identity, scale)
        {


        }        

        protected override void InitializeModel() {
            setFirstPosition();

            initializeGround();
        }

        private void initializeGround() {
            vertices = new VertexPositionColor[numQuads * numQuads * 4];
            //6 vertices make up 2 triangles which make up our rectangle
            indices = new int[numQuads * numQuads * 6];

            int verticeCounter = -1;
            int indiceCounter = -1;

            //Go through each row
            Color rowColor = Color.White;
            for(int row = 0; row < numQuads; row++) {

                //Go through each column, starting with the rowColor
                Color colColor = rowColor;
                for(int col = 0; col < numQuads; col++) {

                    //Get positions for the 4 vertices of the quad
                    QuadVerticePositions qvp = new QuadVerticePositions(nextPosition(row, col), this.QuadRadius);

                    verticeCounter++;
                    indiceCounter++;

                    // Top left
                    vertices[verticeCounter] = new VertexPositionColor(qvp.topLeft, colColor);
                    indices[indiceCounter] = verticeCounter;

                    // Top right
                    verticeCounter++;
                    vertices[verticeCounter] = new VertexPositionColor(qvp.topRight, colColor);
                    indices[indiceCounter + 1] = verticeCounter;
                    indices[indiceCounter + 3] = verticeCounter;

                    // Bottom Left
                    verticeCounter++;
                    vertices[verticeCounter] = new VertexPositionColor(qvp.bottomLeft, colColor);
                    indices[indiceCounter + 2] = verticeCounter;
                    indices[indiceCounter + 5] = verticeCounter;

                    // Bottom right
                    verticeCounter++;
                    vertices[verticeCounter] = new VertexPositionColor(qvp.bottomRight, colColor);
                    indices[indiceCounter + 4] = verticeCounter;

                    indiceCounter += 5;

                    if(colColor == Color.White) {
                        colColor = Color.Black;
                    } else {
                        colColor = Color.White;
                    }
                }

                //Switch starting color
                if(rowColor == Color.White) {
                    rowColor = Color.Black;
                } else {
                    rowColor = Color.White;
                }
            }
        }

        private void setFirstPosition() {
            //Calculate shift distance
            float shift = (float)(this.numQuads - 1 / 2) * this.QuadRadius;

            this.firstQuadPosition = new Vector3(this.Position.X + shift, this.Position.Y, this.Position.Z + shift);
        }

        private Vector3 nextPosition(int row, int col) {
            float x = (QuadRadius * 2 * row);
            float z = (QuadRadius * 2 * col);

            return new Vector3(
                firstQuadPosition.X - (row * QuadRadius * 2) - QuadRadius,
                this.Position.Y,
                firstQuadPosition.Z - (col * QuadRadius * 2) - QuadRadius);
        }
    }
}
